﻿//+-------------------------------------------------------------------------------------------------------------------------------------------------------
//
//Description: DeployResultsPageViewModel
//Copyright © Microsoft Corporation
//Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
//You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
//
//THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, 
//INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. 
//See the Apache Version 2.0 License for specific language governing permissions and limitations under the License.
// 
//--------------------------------------------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;
using VmroleAuthor.Commands;

namespace VmroleAuthor.ViewModels
{
    public class DeployResultsPageViewModel : DeployPageViewModelBase
    {
        private readonly Window deployWindow;
        public DeployResultsPageViewModel(VmRoleResourceExtensionViewModel resExtViewModel,
            ResExtAzureDeploymentData deployData, Window deployWindow )
            : base(resExtViewModel, deployData)
        {
            this.DeployData.DeploymentUpdated += this.DeploymentUpdated;
            this.PageTitle = "DEPLOYMENT RESULTS";
            this.deployWindow = deployWindow;
            this.CopyCommand = new RelayCommand(param => this.CopyMessages(), param => this.CanCopyMessages());
        }

        public static class PropertyNames
        {
            public const string ResExtDeploymentParamSettingViewModels = "ResExtDeploymentParamSettingViewModels";
            public const string IsUserInputComplete = "IsUserInputComplete";
            public const string DeploymentProgressTitle = "DeploymentProgressTitle";
            public const string DeploymentResultSummaryTitle = "DeploymentResultSummaryTitle";
            public const string SummaryResultMessage = "SummaryResultMessage";
            public const string AdditionalResultMessages = "AdditionalResultMessages";
            public const string CopyButtonVisibility = "CopyButtonVisibility";
        }  
   
        public ResExtAzureDeploymentStatus ResExtAzureDeploymentStatus
        {
            get { return this.DeployData.ResExtAzureDeploymentStatus; }
        }

        public string DeploymentResultSummaryTitle
        {
            get
            {
                if (this.DeployData.ResExtAzureDeploymentStatus == ResExtAzureDeploymentStatus.InProgress)
                {
                    return "DEPLOYMENT IN PROGRESS, PLEASE DO NOT CLOSE THIS WINDOW UNTIL DEPLOYMENT IS COMPLETE";
                }
                return "DEPLOYMENT SUMMARY";
            }
        }

        public ResExtDeploymentResult SummaryResultMessage
        {
            get { return this.DeployData.SummaryResultMessage; }
        }

        public  ObservableCollection<ResExtDeploymentResult> AdditionalResultMessages
        {
            get { return this.DeployData.AdditionalResultMessages; }
        }

        internal override bool IsUserInputComplete
        {
            get
            {
                if (this.DeployData.ResExtAzureDeploymentStatus == ViewModels.ResExtAzureDeploymentStatus.InProgress)
                {
                    return false;
                }
                return true;   
            }
        }

        public object CopyButtonVisibility
        {
            get
            {
                return ((this.IsUserInputComplete) ? Visibility.Visible : Visibility.Hidden);
            }
        }

        public ICommand CopyCommand
        {
            get;
            private set;
        }

        public bool CanCopyMessages()
        {
            return (this.IsUserInputComplete);
        }

        private void CopyMessages()
        {
            System.Windows.Forms.Clipboard.SetText(this.GetMessages());
        }

        private string GetMessages()
        {
            string messages = string.Empty;
            if (this.SummaryResultMessage != null)
            {
                messages = "SummaryResult : " + this.SummaryResultMessage.Category.ToString() + Environment.NewLine;
                messages = messages + "SummaryMessage : " + this.SummaryResultMessage.Message + Environment.NewLine;
            }

            if (this.AdditionalResultMessages != null)
            {
                foreach (ResExtDeploymentResult depResult in this.AdditionalResultMessages)
                {
                    messages = messages + "SummaryResult : " + depResult.Category.ToString() + Environment.NewLine;
                    messages = messages + "SummaryMessage : " + depResult.Message + Environment.NewLine;
                }
            }

            return messages;
        }

        internal override bool PerformNextStep()
        {
            if (this.deployWindow != null)
            {
                this.deployWindow.Close();
            }
            return true;
        }

        internal override string GetSettings()
        {
            return this.DeployData.AzureResExtProtectedSettings;
        }

        internal override string GetPowerShellScript()
        {
            return AzureHelper.GetPowerShellScript(this.DeployData);
        }

        private void DeploymentUpdated(object sender, EventArgs e)
        {
            this.OnPropertyChanged(PropertyNames.DeploymentProgressTitle);
            this.OnPropertyChanged(PropertyNames.DeploymentResultSummaryTitle);
            this.OnPropertyChanged(PropertyNames.SummaryResultMessage);
            this.OnPropertyChanged(PropertyNames.AdditionalResultMessages);
            this.OnPropertyChanged(PropertyNames.CopyButtonVisibility);
        }
    }
}
